import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import commonRouter from './modules/common'
import nestedRouter from './modules/nested'
import permissionRouter from './modules/permission'

/**
 * 路由相关属性说明
 * hidden: 当设置hidden为true时，不在侧边栏中显示
 * linkUrl：当设置为true时，导航到第三方网址
 * mete{
 *  title: xxx,    侧边栏名称
 *  icon: xxx,     侧边栏图标
 *  noCache: true  当设置为true时不缓存该路由页面
 * }
 */

// 公共路由
export const commonRoutes = [...commonRouter]

// 异步路由
export const asyncRoutes = [
  ...permissionRouter,
  ...nestedRouter
  // {
  //   path: '*',
  //   redirect: '/404',
  //   hidden: true
  // }
]

const createRouter = () => {
  return new VueRouter({
    routes: commonRoutes,
    scrollBehavior: () => ({ y: 0 })
  })
}

const router = createRouter()

// 解决addRoute不能删除动态路由问题
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

// vue-router 3.x 监听错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
